package snooker.scoreboard;

import java.util.LinkedList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter {
	
	private List<SnookerCsvFile> fileList;
	
	public FileListAdapter(String[] fileNames) {
		fileList = new LinkedList<SnookerCsvFile>();
		for (String fn: fileNames) {
			String[] s = fn.replace(".csv", "").split("_match_");
			SnookerCsvFile scf = new SnookerCsvFile();
			scf.fileName = fn;
			if (s.length == 2) {
				scf.players = s[0];
				String dts = s[1];
				String[] dtsl = dts.split("_");
				if (dtsl.length == 6) {
					scf.dateTime = 
						dtsl[0] + "-" +
						dtsl[1] + "-" +
						dtsl[2] + " " +
						dtsl[3] + ":" +
						dtsl[4] + ":" +
						dtsl[5];
				} else {
					scf.dateTime = dts;
				}
			}
			fileList.add(scf);
		}
	}
	
	public int getCount() {
		return fileList.size();
	}

	public Object getItem(int position) {
		return fileList.get(position).fileName;
	}

	public long getItemId(int position) {
		return fileList.get(position).hashCode();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		SnookerCsvFile scf = fileList.get(position);
		String text = "";
		if (scf.players == null) {
			text = scf.fileName.replace(".csv", "");
		} else {
			text = scf.players + "\n" + scf.dateTime;
		}
		TextView tv = new TextView(parent.getContext());
		tv.setText(text);
		return tv;
	}

	class SnookerCsvFile {
		String fileName;
		String players;
		String dateTime;
	}
}
